package com.hanbang.chu_xing_gao_zhao.view_find_esoterica;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.application.MyApplication;
import com.hanbang.chu_xing_gao_zhao.base.BaseDataActivity;
import com.hanbang.chu_xing_gao_zhao.entity.FindUploadActivityKData;
import com.hanbang.chu_xing_gao_zhao.myview.GridViewForScrollView;
import com.hanbang.chu_xing_gao_zhao.myview.ListViewForScrollView;
import com.hanbang.chu_xing_gao_zhao.myview.MorePopupWindowPublic_B;
import com.hanbang.chu_xing_gao_zhao.myview.MyDialog;
import com.hanbang.chu_xing_gao_zhao.myview.MyDialog.MODE;
import com.hanbang.chu_xing_gao_zhao.utils.MyLog;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.chu_xing_gao_zhao.utils.MySharedPreferences;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.chu_xing_gao_zhao.utils.UiUtils;
import com.hanbang.chu_xing_gao_zhao.view_public.SucceedHintActivity;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FindEsotericaUploadingActivityK extends BaseDataActivity {
    /**
     * 图片展示
     */
    private ImageView picture;
    /**
     * 提示添加图片
     */
    private LinearLayout hintAddPicture;
    /**
     * 提示添加视频
     */
    private LinearLayout hintAddVideo;
    /**
     * 视频
     */
    private VideoView video;
    /**
     * 名称
     */
    private EditText name;
    /**
     * 特色
     */
    private EditText feature;
    /**
     * 价格
     */
    private EditText price;
    /**
     * 流程
     */
    private EditText process;
    /**
     * 注意事项
     */
    private EditText attention;
    /**
     * 提交
     */
    private TextView commit;

    /**
     * 过程图片
     */
    private GridViewForScrollView processPicture;

    /**
     * 图片路径
     */
    private String path = "";

    /**
     * 图片路径
     */
    private String pathTitle = "";
    /**
     * 视频路径
     */
    private String pathVideo = "";
    /**
     * 视频控制
     */
    private MediaController mController;
    /**
     * 视频文件
     */
    private File fileVideo;

    /**
     * 过程图片展示
     */
    private ArrayList<String> pictureShowData = new ArrayList<String>();
    /**
     * 过程图片adapter
     */
    private PictureShowAdapter pAdapter;

    /**
     * 主料
     */
    private ListViewForScrollView zhuLiaoList;

    /**
     * 辅料
     */
    private ListViewForScrollView fuLiaolist;

    /**
     * 添加主料
     */
    private LinearLayout addZhuLiao;

    /**
     * 添加辅料
     */
    private LinearLayout addFuLiao;

    /**
     * 主料集合
     */
    private ArrayList<FindUploadActivityKData> mainListData = new ArrayList<FindUploadActivityKData>();

    /**
     * 主料的adapter
     */
    private AddDataAdapter mainAdapter;
    /**
     * 辅料的adapter
     */
    private AddDataAdapter assistAdapter;

    /**
     * 辅料集合
     */
    private ArrayList<FindUploadActivityKData> assistListData = new ArrayList<FindUploadActivityKData>();

    /**
     * 主料
     */
    private StringBuffer mainMaterial = new StringBuffer();

    /**
     * 辅料
     */
    private StringBuffer assistMaterial = new StringBuffer();


    /**
     * 菜系选择
     */
    private TextView styleOfCooking;

    private ScrollView sv;
    /**
     * 费用模试
     * 免费的为0
     */
    private int modelCost = 0;
    /**
     * 免费
     */
    private TextView free;
    /**
     * 收费
     */
    private TextView charge;
    /**
     * 菜系ID
     */
    private int cuisines_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_uploading_k_activity);
        initView();
        setOnClickListener();
        addData(2);//初始化显示2行
    }


    /**
     * 初始化view
     */
    private void initView() {
        setTitleMore(View.VISIBLE, View.INVISIBLE, "上传开放式秘籍", null, null, null);
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));

        // 控件初始化
        sv = (ScrollView) findViewById(R.id.sv_find_uploading_k_activity_scrollview);


        picture = (ImageView) findViewById(R.id.iv_find_uploading_k_page_1_picture);
        hintAddPicture = (LinearLayout) findViewById(R.id.ll_find_uploading_k_page_1_hintAddPicture);
        hintAddVideo = (LinearLayout) findViewById(R.id.ll_find_uploading_k_activity_hintAddVidei);
        video = (VideoView) findViewById(R.id.vv_find_uploading_k_activity_video);
        name = (EditText) findViewById(R.id.et_find_uploading_k_page_1_name);
        feature = (EditText) findViewById(R.id.et_find_uploading_k_page_1_feature);
        process = (EditText) findViewById(R.id.et_find_uploading_k_activity_describe);
        price = (EditText) findViewById(R.id.et_find_uploading_k_activity_price);
        attention = (EditText) findViewById(R.id.et_find_uploading_k_activity_zhuYiShiXiang);
        commit = (TextView) findViewById(R.id.tv_find_uploading_k_activity_commint);
        processPicture = (GridViewForScrollView) findViewById(R.id.gv_find_uploading_k_activity_processPicture);
        styleOfCooking = (TextView) findViewById(R.id.tv_find_uploading_k_stylefCooking);

        free = (TextView) findViewById(R.id.tv_find_uploading_k_activity_mianFei);
        charge = (TextView) findViewById(R.id.tv_find_uploading_k_activity_shouFei);

        pAdapter = new PictureShowAdapter(pictureShowData);
        processPicture.setAdapter(pAdapter);

        zhuLiaoList = (ListViewForScrollView) findViewById(R.id.lv_find_upload_k_page_1_zhuLiao);
        fuLiaolist = (ListViewForScrollView) findViewById(R.id.lv_find_upload_k_page_1_fuLiao);

        addZhuLiao = (LinearLayout) findViewById(R.id.ll_find_upload_k_page_1_zhuLiao);
        addFuLiao = (LinearLayout) findViewById(R.id.ll_find_upload_k_page_1_fuLiao);

        mainAdapter = new AddDataAdapter(1);
        zhuLiaoList.setAdapter(mainAdapter);

        assistAdapter = new AddDataAdapter(2);
        fuLiaolist.setAdapter(assistAdapter);

        sv.smoothScrollTo(0, 0);
    }

    /**
     * @param num 1,2为初始化2行显示
     *            2，0为添加主料
     *            3，1为添加辅料
     */
    private void addData(int num) {
        if (num == 2) {
            for (int i = 0; i < num; i++) {
                mainListData.add(new FindUploadActivityKData());
                assistListData.add(new FindUploadActivityKData());
            }
        } else if (num == 0) {
            mainListData.add(new FindUploadActivityKData());
            mainAdapter.notifyDataSetChanged();
        } else if (num == 1) {
            assistListData.add(new FindUploadActivityKData());
            assistAdapter.notifyDataSetChanged();

        }


    }

    /**
     * 添加视频资源
     *
     * @param url
     */
    private void playVideo(String url) {
        // 控制视频播放
        mController = new MediaController(this);

        fileVideo = new File(pathVideo);

        if (fileVideo.exists()) {
            // 设置播放视频源的路径
            video.setVideoPath(fileVideo.getAbsolutePath());
            // 为VideoView指定MediaController
            video.setMediaController(mController);

            // 为MediaController指定控制的VideoView
            mController.setMediaPlayer(video);

            // 增加监听上一个和下一个的切换事件，默认这两个按钮是不显示的
            mController.setPrevNextListeners(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(FindEsotericaUploadingActivityK.this, "下一个", Toast.LENGTH_SHORT).show();
                }
            }, new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(FindEsotericaUploadingActivityK.this, "上一个", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        picture.setOnClickListener(onClickListener);
        hintAddPicture.setOnClickListener(onClickListener);
        hintAddVideo.setOnClickListener(onClickListener);
        video.setOnClickListener(onClickListener);
        commit.setOnClickListener(onClickListener);
        addZhuLiao.setOnClickListener(onClickListener);
        addFuLiao.setOnClickListener(onClickListener);
        styleOfCooking.setOnClickListener(onClickListener);

        free.setOnClickListener(onClickListener);
        charge.setOnClickListener(onClickListener);


    }

    /**
     * 处理监听
     */
    private OnClickListener onClickListener = new OnClickListener() {


        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.ll_find_upload_k_page_1_fuLiao:
                    addData(1);
                    break;
                case R.id.ll_find_upload_k_page_1_zhuLiao:
                    addData(0);
                    break;
                //选择菜系
                case R.id.tv_find_uploading_k_stylefCooking:
                    MorePopupWindowPublic_B popup = new MorePopupWindowPublic_B(FindEsotericaUploadingActivityK.this, styleOfCookingList, 8, 100);
                    popup.showPopupWindow(styleOfCooking, 0.7);
                    popup.setOnClickCallback(new MorePopupWindowPublic_B.OnClickCallback() {
                        @Override
                        public void onClick(MorePopupWindowPublic_B morePopWindow, int id, String content, int type) {
                            styleOfCooking.setText(content);
                            cuisines_id = id;
                            MyLog.e("text", "菜系id" + cuisines_id);
                        }
                    });
                    break;


                //选择免费
                case R.id.tv_find_uploading_k_activity_mianFei:
                    modelCost = 0;
                    free.setBackgroundResource(R.drawable.bg_corners12__yellow_background);
                    charge.setBackgroundResource(R.drawable.bg_corners12_graydd_background);
                    price.setFocusable(false);
                    price.setFocusableInTouchMode(false);
                    price.setText("");
                    break;
                //选择收费
                case R.id.tv_find_uploading_k_activity_shouFei:
                    modelCost = 1;
                    free.setBackgroundResource(R.drawable.bg_corners12_graydd_background);
                    charge.setBackgroundResource(R.drawable.bg_corners12__yellow_background);
                    price.setFocusable(true);
                    price.setFocusableInTouchMode(true);
                    break;


                // 添加视频
                case R.id.ll_find_uploading_k_activity_hintAddVidei:

                    // 新建一个dialog 对话框
                    MyDialog myDialogVideo = new MyDialog(FindEsotericaUploadingActivityK.this, R.style.about_me_dialog, MODE.HEAD);
                    // 添加图片的监听
                    myDialogVideo.setOnClickCallback(new com.hanbang.chu_xing_gao_zhao.myview.MyDialog.OnClickCallback() {

                        @Override
                        public void onClick(MyDialog dialog, Object number, Object content) {

                            // 录像
                            if ((Integer) number == 1) {
                                pathVideo = "";
                                File fDir = new File(MyApplication.picFilepath);
                                pathVideo = System.currentTimeMillis() + ".mp4";
                                if (fDir.exists() || fDir.mkdirs()) {
                                    File cameraFile = new File(fDir, pathVideo);
                                    if (!cameraFile.exists()) {
                                        try {
                                            cameraFile.createNewFile();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
                                    startActivityForResult(intent, 996);
                                }
                                // 相册选择
                            } else if ((Integer) number == 2) {
                                // 获取相册
                                Intent intent = new Intent();
                                intent.setType("video/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.putExtra("return-data", true);

                                startActivityForResult(intent, 997);
                            }

                        }
                    });
                    myDialogVideo.show();
                    break;
                // 提交
                /**
                 * type：类型，1为开放式，2为买断式
                 *
                 * title：秘籍名称
                 *
                 * tedian：特点
                 *
                 * price：价格，免费的传0
                 *
                 * cuisines_id：菜系，这个在效果图上没有体现出来，需要加上去的
                 *
                 * img_url:主图，秘籍效果图，一张">url:主图，秘籍效果图，一张
                 *
                 * shipin:视频，可选
                 *
                 * zhuliao：主料，格式为：（名称:数量|名称:数量）
                 *
                 * fuliao：辅料，格式为：（名称:数量|名称:数量）
                 *
                 * gongyiliucheng：工艺流程
                 *
                 * zhuyishixiang：注意事项
                 *
                 * img_guocheng：过程图片，可为多张
                 */
                case R.id.tv_find_uploading_k_activity_commint:


                    if (pathTitle.equals("")) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "图片不能为空", 1);
                        return;
                    } else if (name.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "名称不能为空", 1);
                        return;
                    } else if (feature.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "特色不能为空", 1);
                        return;

                    } else if (process.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "流程没有填写", 1);
                        return;
                    } else if (true) {
                        for (int i = 0; i < mainListData.size() - 1; i++) {
                            if (mainListData.get(i).getName().equals("")) {

                                MyToast.show(FindEsotericaUploadingActivityK.this, "主料不能为空", 1);
                                return;
                            }
                        }

                        for (int i = 0; i < assistListData.size() - 1; i++) {
                            if (assistListData.get(i).getName().equals("")) {
                                MyToast.show(FindEsotericaUploadingActivityK.this, "辅料料不能为空", 1);
                                return;
                            }
                        }
                    }

                    if (modelCost == 1 && price.getText().toString().equals("")) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "收费模式价格不能为空", Toast.LENGTH_SHORT);
                        return;
                    }


                    // 通过图片路径得到一个图片文件
                    File picture = new File(pathTitle);

                    //主料赋值
                    for (int i = 0; i < mainListData.size(); i++) {
                        mainMaterial.append(mainListData.get(i).getName() + ":" + mainListData.get(i).getWeight() + "|");
                    }
                    //辅料赋值
                    for (int i = 0; i < assistListData.size(); i++) {
                        assistMaterial.append(assistListData.get(i).getName() + ":" + assistListData.get(i).getWeight() + "|");
                    }

//                    MyLog.i("text", "111111***" + mainMaterial.toString());
//                    MyLog.i("text", "222222***" + assistMaterial.toString());

                    //添加参数
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("telphone", MySharedPreferences.getSharedPreferencesKeyAndValue(
                            FindEsotericaUploadingActivityK.this, MySharedPreferences.USER_DATA, "tlephone"));
                    params.addBodyParameter("password", MySharedPreferences.getSharedPreferencesKeyAndValue(
                            FindEsotericaUploadingActivityK.this, MySharedPreferences.USER_DATA, "password"));
                    params.addBodyParameter("type", 1 + "");
                    params.addBodyParameter("title", name.getText().toString());
                    params.addBodyParameter("tedian", feature.getText().toString());
                    params.addBodyParameter("price", price.getText().toString());
                    params.addBodyParameter("img_url", picture);
                    params.addBodyParameter("cuisines_id", cuisines_id + "");

                    //视频不空则上传
                    if (pathVideo != "") {

                        params.addBodyParameter("shipin", fileVideo);
                    }
                    params.addBodyParameter("maiduanliucheng", process.getText().toString());
                    params.addBodyParameter("zhuyishixiang", attention.getText().toString());
                    params.addBodyParameter("zhuliao", mainMaterial.toString());
                    params.addBodyParameter("fuliao", assistMaterial.toString());

                    for (int i = 0; i < pictureShowData.size(); i++) {
                        File img_guocheng = new File(pictureShowData.get(i));
                        params.addBodyParameter("img_guocheng", img_guocheng);

                    }


                    httpUploading(HttpInterfaces.Miji_Add, params);

                    break;
                // 添加图片
                case R.id.ll_find_uploading_k_page_1_hintAddPicture:
                    // 新建一个dialog 对话框
                    MyDialog myDialog = new MyDialog(FindEsotericaUploadingActivityK.this, R.style.about_me_dialog, MODE.HEAD);
                    // 添加图片的监听
                    myDialog.setOnClickCallback(new com.hanbang.chu_xing_gao_zhao.myview.MyDialog.OnClickCallback() {

                        @Override
                        public void onClick(MyDialog dialog, Object number, Object content) {

                            // 拍照
                            if ((Integer) number == 1) {
                                pathTitle = "";
                                File fDir = new File(MyApplication.picFilepath);
                                pathTitle = System.currentTimeMillis() + ".jpg";
                                if (fDir.exists() || fDir.mkdirs()) {
                                    File cameraFile = new File(fDir, pathTitle);
                                    if (!cameraFile.exists()) {
                                        try {
                                            cameraFile.createNewFile();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
                                    startActivityForResult(intent, 998);
                                }
                                // 相册选择
                            } else if ((Integer) number == 2) {
                                // 获取相册
                                Intent intent = new Intent();
                                intent.setType("image/*");

                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.putExtra("return-data", true);
                                startActivityForResult(intent, 999);
                            }

                        }
                    });
                    myDialog.show();
                    break;
                default:
                    break;
            }

        }

    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        /**
         * 获取视频地址
         */
        if (requestCode == 997 && resultCode == -1) {
            Uri uri = data.getData();
            pathVideo = SystemTools.getPath(FindEsotericaUploadingActivityK.this, uri);
            // file = new File(pathVideo);
            hintAddVideo.setVisibility(View.GONE);
            playVideo(pathVideo);
            // 获取拍照的图片路径
        } else if (requestCode == 996 && resultCode == -1) {
            File fDir = new File(MyApplication.picFilepath);
            // 判断文件夹是否存在
            if (fDir.exists() || fDir.mkdirs()) {
                File cameraFile = new File(fDir, pathVideo);
                // 判断文件时是否存在
                if (cameraFile.exists()) {
                    pathVideo = cameraFile.getPath();
                    // file = new File(pathVideo);
                    hintAddVideo.setVisibility(View.GONE);
                    playVideo(pathVideo);
                } else {
                    MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
                }
            } else {
                MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
            }
        }

        /**
         * 获取图片地址
         */
        if (requestCode == 999 && resultCode == -1) {
            Uri uri = data.getData();
            pathTitle = SystemTools.getPath(FindEsotericaUploadingActivityK.this, uri);
            bitmapUtils.display(picture, pathTitle);
            hintAddPicture.setVisibility(View.GONE);
            // 获取拍照的图片路径
        } else if (requestCode == 998 && resultCode == -1) {
            File fDir = new File(MyApplication.picFilepath);
            // 判断文件夹是否存在
            if (fDir.exists() || fDir.mkdirs()) {
                File cameraFile = new File(fDir, pathTitle);
                // 判断文件时是否存在
                if (cameraFile.exists()) {
                    pathTitle = cameraFile.getPath();
                    bitmapUtils.display(picture, pathTitle);
                    hintAddPicture.setVisibility(View.GONE);
                } else {
                    MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
                }
            } else {
                MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
            }
        }

        /**
         * 获取 流程 图片地址
         */
        if (requestCode == 981 && resultCode == -1) {
            Uri uri = data.getData();
            path = SystemTools.getPath(FindEsotericaUploadingActivityK.this, uri);
            pictureShowData.add(path);
            pAdapter.notifyDataSetChanged();

            // 获取拍照的图片路径
        } else if (requestCode == 980 && resultCode == -1) {
            File fDir = new File(MyApplication.picFilepath);
            // 判断文件夹是否存在
            if (fDir.exists() || fDir.mkdirs()) {
                File cameraFile = new File(fDir, path);
                // 判断文件时是否存在
                if (cameraFile.exists()) {
                    path = cameraFile.getPath();

                    pictureShowData.add(path);
                    pAdapter.notifyDataSetChanged();

                } else {
                    MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
                }
            } else {
                MyToast.show(FindEsotericaUploadingActivityK.this, "获取照片失败！", Toast.LENGTH_SHORT);
            }
        }

    }

    ;

    /**
     * 提交上传秘籍
     */
    private void httpUploading(String url, RequestParams params) {
        httpUtils.send(HttpMethod.POST, url, params, new MyRequestCallBack<String>(this, "提交中...") {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);

                    int result = json.getInt("Result");
                    if (result == 0) {
                        Intent intent = new Intent(FindEsotericaUploadingActivityK.this, SucceedHintActivity.class);
                        intent.putExtra("title", "提交成功");
                        intent.putExtra("contentHint", "感谢您的参与和分享\n我们将24小时审核完成");
                        startActivity(intent);
//                        MyLog.i("text", "22222***" + result + "***");
                        finish();
                    } else {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "服务器忙，请稍后重试..", 1);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);

//                MyLog.e("text", "total=" + total + "current=" + current);


            }
        });
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    /**
     * 图片展示 adapter
     *
     * @author Administrator
     */
    class PictureShowAdapter extends BaseAdapter {
        public ArrayList<String> list;

        public PictureShowAdapter(ArrayList<String> list) {

            if (list != null) {
                this.list = list;

            } else {
                list = new ArrayList<String>();

            }
        }

        @Override
        public int getCount() {
            return list.size() + 1;
        }

        @Override
        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            UiUtils.applyFont(convertView);
            ViewHolder viewHolder;
            if (convertView == null) {// 是空加载
                convertView = getLayoutInflater().inflate(R.layout.item_dycamic_horizontal_listview_delect, null);
                viewHolder = new ViewHolder();
                viewHolder.picture = (ImageView) convertView.findViewById(R.id.iv_item_shoppingcart_recommend_image);
                viewHolder.deletePicture = (ImageView) convertView
                        .findViewById(R.id.iv_item_shoppingcart_recommend_delete);
                convertView.setTag(viewHolder);
            } else {// 不是空复用

                viewHolder = (ViewHolder) convertView.getTag();
            }

            // 添加加号
            if (position == list.size()) {
                viewHolder.picture.setPadding(10, 70, +70 + 60, 70);
                viewHolder.picture.setImageResource(R.drawable.add);
                viewHolder.deletePicture.setVisibility(View.GONE);
                // 添加图片
            } else {
                viewHolder.picture.setPadding(0, 0, 0, 00);
                viewHolder.deletePicture.setVisibility(View.VISIBLE);
                bitmapUtils.display(viewHolder.picture, list.get(position));
            }


            viewHolder.picture.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (pictureShowData.size() == 9) {
                        MyToast.show(FindEsotericaUploadingActivityK.this, "图片最多上传9张", Toast.LENGTH_SHORT);
                        return;
                    }
                    // 新建一个dialog 对话框
                    MyDialog myDialog = new MyDialog(FindEsotericaUploadingActivityK.this, R.style.about_me_dialog, MODE.HEAD);
                    // 添加图片的监听
                    myDialog.setOnClickCallback(new com.hanbang.chu_xing_gao_zhao.myview.MyDialog.OnClickCallback() {

                        @Override
                        public void onClick(MyDialog dialog, Object number, Object content) {
                            path = "";
                            // 拍照
                            if ((Integer) number == 1) {
                                File fDir = new File(MyApplication.picFilepath);
                                path = System.currentTimeMillis() + ".jpg";
                                if (fDir.exists() || fDir.mkdirs()) {
                                    File cameraFile = new File(fDir, path);
                                    if (!cameraFile.exists()) {
                                        try {
                                            cameraFile.createNewFile();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
                                    startActivityForResult(intent, 980);
                                }
                                // 相册选择
                            } else if ((Integer) number == 2) {
                                // 获取相册
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                intent.putExtra("return-data", true);
                                startActivityForResult(intent, 981);
                            }

                        }
                    });
                    myDialog.show();
                }
            });

            // 删除图片
            viewHolder.deletePicture.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    pictureShowData.remove(position);
                    pAdapter.notifyDataSetChanged();

                }
            });

            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            private ImageView picture; // 图片
            private ImageView deletePicture; // 删除 图片


        }

    }


    /**
     * 主料，辅料，添加数据 adapter
     *
     * @author Administrator
     */
    class AddDataAdapter extends BaseAdapter {
        int type;

        int p;

        public AddDataAdapter(int type) {
            this.type = type;
        }


        @Override
        public int getCount() {
            return type == 1 ? mainListData.size() : assistListData.size();
        }

        @Override
        public FindUploadActivityKData getItem(int position) {
            return type == 1 ? mainListData.get(position) : assistListData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            UiUtils.applyFont(convertView);
            ViewHolder viewHolder;

            /**
             * 取消重复利用的机制
             */
            convertView = getLayoutInflater().inflate(R.layout.item_find_uploading_k, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (EditText) convertView.findViewById(R.id.ev_find_uploading_k_name);
            viewHolder.weight = (EditText) convertView.findViewById(R.id.ev_find_uploading_k_weight);


            final FindUploadActivityKData data = getItem(position);
            /**
             * name 赋值
             */
            viewHolder.name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    getItem(position).setName(s.toString());
//                    MyLog.i("text", "111111***" + position + "***" + s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            /**
             * weight 赋值
             */
            viewHolder.weight.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    getItem(position).setWeight(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });


            viewHolder.name.setText(data.getName());
            viewHolder.weight.setText(data.getWeight());
            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            private EditText name;
            private EditText weight;


        }

    }


}
